<?php
namespace app\index\controller;

use app\common\model\CompanyConfession as CompanyConfessionModel;

/**
 * 忏悔控制器
 * Class Companyconfession
 * @package app\index\controller
 */
class Companyconfession extends Base
{
    /**
     * [index  忏悔列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new CompanyConfessionModel;
        $this->getPageAndSize($param);
        $user = $this->user;
        $map = [];
        try{
            if (!empty($param['username'])) {
                $map['m.username'] = ['like', "%" . $param['username'] . "%"];
            }
            if (!empty($param['tel'])) {
                $map['m.tel'] = ['like', "%" . $param['tel'] . "%"];
            }
            if (!empty($param['sex'])) {
                $map['m.sex'] = $param['sex'];
            }
            if (!empty($param['status'])) {
                if($param['status'] == 1){
                    $map['c.status'] = 1;
                }else{
                    $map['c.status'] = 0;
                }
            }
            $map['c.comid'] = $user['comid'];
            $field = 'c.id,m.img,m.username,m.tel mtel,m.sex,c.content,c.create_time,c.status,c.need_kaishi,c.is_ni';
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size,$field);
            foreach($lists as &$v){
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('index.domain_name').$v['img'];
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看礼佛心灯');
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [kaishi  开示]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function kaishi()
    {
        $param = input('param.');
        $model = new CompanyConfessionModel;
        $user = $this->user;
        try{
            $param['status'] = 1;
            $model->editData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        writelog($user['mid'], $user['comid'], '开示成功');
        return admin_json(1, '开示成功');
    }
}